package com.level2app.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;

import androidx.annotation.Nullable;


import com.level2app.base.BaseActivity;
import com.level2app.base.BaseViewModel;
import com.level2app.databinding.ActivitySplashBinding;
import com.level2app.ui.app.MainActivity;
import com.level2app.ui.login.CheckEmailActivity;
import com.level2app.ui.welcome.WelcomeActivity;
import com.level2app.utils.JumpIntent;

/**
 * 启动页
 */
public class SplashActivity extends BaseActivity<ActivitySplashBinding, BaseViewModel> {

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        // 避免从桌面启动程序后，会重新实例化入口类的activity
//        try {
//            if (!this.isTaskRoot()) {
//                Intent intent = getIntent();
//                if (intent != null) {
//                    String action = intent.getAction();
//                    if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
//                        finish();
//                        return;
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        handleLink();
//    }

    @Override
    protected void initData() {
        new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
//                JumpIntent.jump(SplashActivity.this, WelcomeActivity.class, true);
                JumpIntent.jump(SplashActivity.this, MainActivity.class, true);

//                boolean isFirst = SPUtil.getBoolean("isFirst", true);
//                if (isFirst) {
//                    ToastHelper.show("用户协议弹窗");
//                } else {
//                    JumpIntent.jump(SplashActivity.this, MyApplication.manager.isLogin()
//                            ? MainActivity.class : LoginActivity.class, true);
//                    JumpIntent.jump(SplashActivity.this, MainActivity.class, true);
//                }
            }
        }.start();

//        new Handler().postDelayed(() -> {
////            JumpIntent.jump(SplashActivity.this, WelcomeActivity.class, true);
//            JumpIntent.jumpToMain(SplashActivity.this, true);
//        }, 3000);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        new Handler().postDelayed(() -> {
//            JumpIntent.jump(SplashActivity.this, MainActivity.class, true);
////            JumpIntent.jump(SplashActivity.this, WelcomeActivity.class, true);
//        }, 3000);
    }

//    @Override
//    protected void onNewIntent(Intent intent) {
//        super.onNewIntent(intent);
//        handleLink();
//    }

    private void handleLink() {
        //APP link
        // ATTENTION: This was auto-generated to handle app links.
        Intent appLinkIntent = getIntent();
        if (appLinkIntent == null) return;
        String appLinkAction = appLinkIntent.getAction();
        Uri appLinkData = appLinkIntent.getData();

        if (appLinkData != null) {
            // 获取URL中的参数等
            appLinkData.getScheme();//获取scheme
            appLinkData.getHost();//获取host
            appLinkData.getAuthority();//获取authority
//            String mailSn = data.getQueryParameter("mailSn");
            if (appLinkData.getHost().equals("level2.com")) {
                JumpIntent.jump(SplashActivity.this, MainActivity.class, true);
            }
        }
    }
}